<?php

namespace App\Console\Commands;

use App\backend\models\GoodsCronTimeModel;
use App\backend\models\GoodsModel;
use Illuminate\Console\Command;

class GoodsMonitorTask extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:con';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // 定时任务执行的逻辑 商品管理--定时上下架商品
        echo "// ---start---\r\n";

        $set_cron_arr = GoodsCronTimeModel::listed();
        echo 'set_cron_arr: '.json_encode($set_cron_arr)."\r\n";

        if (!empty($set_cron_arr)) {
            foreach ($set_cron_arr as $value) {
                $data = object_to_array($value);
                echo 'data: '. json_encode($data)."\r\n";
                if ($data['listing_time'] != 0) {
                    $where_data[] = ['id', '=', $data['goods_id']];
                    $update_state = GoodsModel::updatedData($where_data, ['status' => 1]);
                } else if ($data['off_shelf_time'] != 0) {
                    $where_data[] = ['id', '=', $data['goods_id']];
                    $update_state = GoodsModel::updatedData($where_data, ['status' => 7]);
                } else {
                    $update_state =  0;
                }

                if ($update_state) {
                    $where_data[] = ['goods_id', '=', $data['goods_id']];
                    GoodsCronTimeModel::updatedData($where_data, ['setting_type' => 0]);
                    echo 'goods_id: '. $data['goods_id']. '更新成功！'."\r\n";
                }
            }
        }

        echo "// ---end---\r\n";

        return 0;
    }
}
